package com.jt.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class UserProxy {
    public static Object getProxy(final Object target){
        ClassLoader cl = target.getClass().getClassLoader();
        Class[] interfaces = target.getClass().getInterfaces();
        Object proxy = Proxy.newProxyInstance(cl, interfaces, new InvocationHandler() {
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                long startTime = System.currentTimeMillis();
                Object result = method.invoke(target,args);
                String className = target.getClass().getSimpleName();
                String methodName = method.getName();
                System.out.println("类名："+className+";"+"方法名："+methodName);
                Thread.sleep(2000);
                long endTime = System.currentTimeMillis();
                System.out.println("总耗时："+(endTime-startTime)+"毫秒");
                return result;
            }
        });
        return proxy;
    }
}
